package com.cershy.linyuminiserver.controller;

import com.cershy.linyuminiserver.annotation.*;
import com.cershy.linyuminiserver.constant.DeviceType;
import com.cershy.linyuminiserver.dto.DeviceDto;
import com.cershy.linyuminiserver.entity.Device;
import com.cershy.linyuminiserver.entity.Message;
import com.cershy.linyuminiserver.service.DeviceService;
import com.cershy.linyuminiserver.utils.ResultUtil;
import com.cershy.linyuminiserver.vo.message.SendMessageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
@Tag(name = "设备管理", description = "设备相关接口")  // 添加这行
public class DeviceController {

    private final DeviceService deviceService;

    @Operation(summary = "添加设备")  // 添加这行
    @PostMapping("/addDevice")
    @UrlFree
    public Object addDevice(@RequestBody DeviceDto deviceAdd) throws Exception{
        return deviceService.addDevice(deviceAdd);
    }

    @Operation(summary = "根据类型获取设备")  // 添加这行
    @PostMapping("/getDevices")
    @UrlFree
    public Object getDevices(@RequestParam DeviceType type,
                             @RequestParam(required = false) Double x,
                             @RequestParam(required = false) Double y,
                             @RequestParam(required = false) Double distance,
                             @RequestParam Integer current
    ) throws Exception{
        return deviceService.getDeviceList(type,x,y,distance,current);
    }

//    @UrlLimit(maxRequests = 100)
//    @PostMapping("/send")
//    //设备更新信息
//    public Object send(@DeviceCode String deviceCode, @UserIp String userIp,
//                       @RequestBody @Valid SendMessageVo sendMessageVo) {
//        sendMessageVo.setUserIp(userIp);
//        Message result = messageService.send(userId, sendMessageVo);
//        return ResultUtil.Succeed(result);
//    }

    @PostConstruct
    public void init() {
        deviceService.addAllDeviceLocation();
    }


}
